from PIL import Image
import ImageEnhance, ImageFilter
import sys

image_name ='captcha.jpg'
im = Image.open(image_name)
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
#im.show()

s = 12
w = 10
h = 15
t = 2
im_new = []

#split four numbers in the picture

for i in range(4):
    im1 = im.crop((s+w*i+i*2, t, s+w*(i+1)+i*2, h))

    im_new.append(im1)

xsize, ysize = im_new[0].size
gd = []
for i in range(ysize):
    tmp = []
    #im_new[k].show()
    for j in range(xsize):
        if(im_new[0].getpixel((j,i)) == 255):
            tmp.append(1)
        else:
            tmp.append(0)
    gd.append(tmp)

for i in range(ysize):
    print gd[i]


            
